package Leetcode.数组字符串;

/**
 * @Author: kirito
 * @Date: 2024/3/11 13:52
 * @Description: TODO
 */

public class searchRange {
    public int[] searchRange(int[] nums, int target) {
        int[] res = {-1, -1};
        int n = nums.length;

        // 查找最后一个<=target 的数的下标
        int l = -1, r = n;
        while(l + 1 < r) {
            int m = l + r >> 1;
            if(nums[m] <= target) l = m;
            else r = m;
        }
        if(l != -1 && nums[l]==target) res[1] = l;

        // 查找第一个>=target 的数的下标
        l = -1; r = n;
        while(l + 1 < r) {
            int m = l + r >> 1;
            if(nums[m] >= target) r = m;
            else l = m;
        }
        if(r != n && nums[r]==target) res[0] = r;
        return res;

    }
}
